import { Lv70DramaMapInfo } from "../data/Lv70DramaMapInfo";
import { SWFPlayer } from "../../../scene/modle/SWFPlayer";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
import { ModelType } from "../../../scene/modle/data/ModelType";
import { TDramaCopy } from "../../../../../Message/Db/Tables/TDramaCopy";
import { ToolTipsManager } from "../../../../../com/mui/manager/ToolTipsManager";
import { Lv70DramaMapTipsRender } from "./Lv70DramaMapTipsRender";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { ObjectPool } from "../../../../../com/gengine/utils/pools/ObjectPool";
import { ObjCreate } from "../../common/ObjCreate";
import { Lv70DramaConfig } from "../../../resource/Lv70DramaConfig";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { Cache } from "../../../cache/Cache";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { Global } from "../../../../../com/gengine/global/Global";
type int = number;
//class Lv70DramaSubMap
    
    export  class Lv70DramaSubMap /* flash.display.Sprite */
    {
        constructor(arg1: Lv70DramaMapInfo, arg2: Function)
        {
            
            this._mapInfo = arg1;
            this._callback = arg2;
            this.initView();
            this.addEventListener(flash.events.Event.ENTER_FRAME, this.onEveryFrame);
            return;
        }

        public get mapId(): int
        {
            return this._mapInfo.id;
        }

        public show(): void
        {
            this.visible = true;
            if (!this.hasEventListener(flash.events.Event.ENTER_FRAME)) 
            {
                this.addEventListener(flash.events.Event.ENTER_FRAME, this.onEveryFrame);
            }
            return;
        }

        public hide(): void
        {
            this.visible = false;
            this.removeEventListener(flash.events.Event.ENTER_FRAME, this.onEveryFrame);
            return;
        }

        public updateMap(arg1: Array<any>, arg2: Array<any>, arg3: int=2): void
        {
            var loc1=false;
            var loc3=false;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            var loc8=null;
            var loc9=false;
            var loc10=null;
            this._mapData = arg2;
            if (arg3 > 0) 
            {
                this._curDifficulty = arg3;
            }
            this._needReload = [];
            var loc2=0;
            while (loc2 < arg1.length) 
            {
                if (this._grayDatas == null || this._grayDatas.length == 0) 
                {
                    this._needReload[loc2] = true;
                }
                else 
                {
                    this._needReload[loc2] = false;
                    loc1 = Boolean(arg1[loc2]);
                    loc3 = Boolean(this._grayDatas[loc2]);
                    if (loc1 != loc3) 
                    {
                        this._needReload[loc2] = true;
                    }
                }
                ++loc2;
            }
            this._grayDatas = arg1;
            loc2 = 0;
            while (loc2 < this._grayDatas.length) 
            {
                loc1 = this._grayDatas[loc2] as Boolean;
                loc4 = this._bosses[loc2] as SWFPlayer;
                loc5 = GTextFiled(this._txtNames[loc2]);
                loc6 = flash.display.Bitmap(this._placeNames[loc2]);
                loc4.visible = true;
                loc5.visible = true;
                loc6.visible = true;
                if (loc1) 
                {
                    loc4.visible = false;
                    loc5.visible = false;
                    loc6.visible = false;
                }
                else if (!loc4.isloadComplete) 
                {
                    loc4.loadComplete = this.onLoadCompleted;
                    loc4.load(this.getSubMapBossFileName(loc2), ModelType.Effect, null);
                }
                ++loc2;
            }
            loc2 = 0;
            while (loc2 < this._needReload.length) 
            {
                loc7 = this._maps[loc2] as Object /* flash.display.Sprite */;
                loc8 = arg2[loc2] as TDramaCopy;
                ToolTipsManager.unregister(loc7);
                ToolTipsManager.register(loc7, Lv70DramaMapTipsRender, loc8);
                if (loc9 = Boolean(this._needReload[loc2])) 
                {
                    if ((loc10 = loc7.getChildAt(0) as SWFPlayer).isloadComplete) 
                    {
                        loc10.dispose(false);
                        loc7.addChildAt(loc10, 0);
                    }
                    loc10.loadComplete = this.onLoadCompleted;
                    loc10.load(this.getSubMapFileName(loc2), ModelType.NormalSwf, null);
                }
                ++loc2;
            }
            return;
        }

        /* internal  */onLoadCompleted(arg1: SWFPlayer): void
        {
            if (arg1.movieClipData) 
            {
                arg1.movieClipData.isClear(false);
            }
            return;
        }

        /* internal  */onEveryFrame(arg1: Object /* flash.events.Event */): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=null;
            var loc4;
            var loc5=((loc4 = this)._curFrame + 1);
            loc4._curFrame = loc5;
            if (this._curFrame % 6 == 0) 
            {
                loc1 = 0;
                while (!(this._maps == null) && loc1 < this._maps.length) 
                {
                    loc2 = this._maps[loc1] as Object /* flash.display.Sprite */;
                    if (!(loc2 == null || loc2.numChildren == 0)) 
                    {
                        if ((loc3 = loc2.getChildAt(0) as SWFPlayer) != null) 
                        {
                            if (this.isMouseUpSwf(loc3)) 
                            {
                                loc2.filters = [FilterConst.worldMapFilterEx];
                            }
                            else 
                            {
                                loc2.filters = [];
                            }
                        }
                    }
                    ++loc1;
                }
            }
            return;
        }

        /* internal  */initView(): void
        {
            var loc1=null;
            var loc2=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            this._bg = ObjectPool.getObject(SWFPlayer);
            this.addChild(this._bg);
            this._bg.loadComplete = this.onLoadCompleted;
            this._bg.load(this.getSubMapBgFileName(), ModelType.NormalSwf, null);
            this._maps = [];
            var loc3=0;
            while (loc3 < this._mapInfo.mapPlaces.length) 
            {
                loc1 = this._mapInfo.mapPlaces[loc3] as Object /* flash.geom.Point */;
                loc2 = ObjectPool.getObject(SWFPlayer);
                (loc4 = new Object /* flash.display.Sprite */()).x = loc1.x;
                loc4.y = loc1.y;
                loc4.addChild(loc2);
                this._maps.push(loc4);
                this.addChild(loc4);
                loc4.addEventListener(flash.events.MouseEvent.CLICK, this.clickEnterCopyHandler);
                ++loc3;
            }
            this._bosses = [];
            this._txtNames = [];
            loc3 = 0;
            while (loc3 < this._mapInfo.bossPlaces.length) 
            {
                loc1 = this._mapInfo.bossPlaces[loc3] as Object /* flash.geom.Point */;
                if (!(loc1 == null || loc1.x == -123 && loc1.y == -123)) 
                {
                    loc2 = ObjectPool.getObject(SWFPlayer);
                    loc2.move(loc1.x - 100, loc1.y - 70);
                    loc2.timeRate = 30;
                    this._bosses.push(loc2);
                    this.addChild(loc2);
                    (loc6 = (loc5 = ObjCreate.createGTextField("", loc1.x - 200, loc1.y - 160, 200, 20, this)).defaultTextFormat).size = 16;
                    loc6.align = flash.text.TextFormatAlign.CENTER;
                    loc6.color = 16164448;
                    loc5.defaultTextFormat = loc6;
                    loc5.htmlText = String(this._mapInfo.bossNames[loc3]);
                    loc5.visible = false;
                    this._txtNames.push(loc5);
                }
                ++loc3;
            }
            this._placeNames = [];
            loc3 = 0;
            while (loc3 < this._mapInfo.mapNamePlaces.length) 
            {
                loc1 = this._mapInfo.mapNamePlaces[loc3] as Object /* flash.geom.Point */;
                (loc7 = new Object /* flash.display.Bitmap */()).x = loc1.x;
                loc7.y = loc1.y;
                this.addChild(loc7);
                this._placeNames.push(loc7);
                ++loc3;
            }
            this._curPlaceIndex = 0;
            this.loadPlaceNames(0);
            return;
        }

        /* internal  */loadPlaceNames(arg1: int): void
        {
            if (arg1 >= this._mapInfo.mapNamePlaces.length) 
            {
                return;
            }
            var loc1="lv70DramaPlaceNames_" + this._mapInfo.id.toString() + "_" + (arg1 + 1).toString() + ".png";
            var loc2=Lv70DramaConfig.instance.getDramaCopy(this._mapInfo.id, this._curDifficulty, arg1 + 1);
            if (loc2 == null) 
            {
                loc1 = "lv70DramaPlaceNamesNotOpened.png";
            }
            LoaderManager.instance.load(loc1, this.onLoadPlaceName);
            return;
        }

        /* internal  */onLoadPlaceName(arg1: ImageInfo): void
        {
            if (this._placeNames == null) 
            {
                return;
            }
            var loc1=this._placeNames[this._curPlaceIndex] as Object /* flash.display.Bitmap */;
            if (loc1 == null) 
            {
                return;
            }
            loc1.bitmapData = arg1.bitmapData;
            var loc2;
            var loc3=((loc2 = this)._curPlaceIndex + 1);
            loc2._curPlaceIndex = loc3;
            this.loadPlaceNames(this._curPlaceIndex);
            return;
        }

        /* internal  */getSubMapFileName(arg1: int): string
        {
            var loc1=Boolean(this._grayDatas[arg1]);
            var loc2=1;
            if (loc1) 
            {
                loc2 = 0;
            }
            return this.MapNamePre + this._mapInfo.id.toString() + "_" + (arg1 + 1).toString() + "_" + loc2.toString() + ".swf";
        }

        /* internal  */getSubMapBgFileName(): string
        {
            return this.MapBgPre + this._mapInfo.id.toString() + "Bg.swf";
        }

        /* internal  */getSubMapBossFileName(arg1: int): string
        {
            return this.MapBossPre + this._mapInfo.id.toString() + "_" + (arg1 + 1).toString() + ".swf";
        }

        /* internal  */mouseOverHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as Object /* flash.display.Sprite */;
            loc1.filters = [FilterConst.worldMapFilter];
            return;
        }

        /* internal  */mouseOutHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as Object /* flash.display.Sprite */;
            loc1.filters = [];
            return;
        }

        /* internal  */clickEnterCopyHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc8=null;
            var loc1=arg1.currentTarget as Object /* flash.display.Sprite */;
            var loc2=-1;
            var loc3=0;
            while (loc3 < this._maps.length) 
            {
                if ((loc8 = this._maps[loc3] as Object /* flash.display.Sprite */).filters.length > 0) 
                {
                    loc2 = loc3 + 1;
                    break;
                }
                ++loc3;
            }
            if (loc2 == -1) 
            {
                return;
            }
            var loc4=this._grayDatas[(loc2 - 1)] as Boolean;
            var loc5;
            if ((loc5 = this._mapData[(loc2 - 1)] as TDramaCopy) == null) 
            {
                return;
            }
            var loc6=Cache.instance.copy.getLv70HightesHistory(loc5.difficulty);
            var loc7;
            if (loc7 = Cache.instance.copy.isLv70DramaTodayDone(loc5.copyCode)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80144));
                return;
            }
            if (loc4) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80123));
                return;
            }
            if (this._callback != null) 
            {
                this._callback.apply(null, [loc2]);
            }
            return;
        }

        /* internal  */isMouseUpSwf(arg1: SWFPlayer): Boolean
        {
            var loc1=0;
            if (!(arg1 == null) && DisplayUtil.isBitmapDataUsable(arg1.bitmapData)) 
            {
                loc1 = arg1.bitmapData.getPixel32(arg1.mouseX, arg1.mouseY);
                return loc1 > 0;
            }
            return arg1.hitTestPoint(Global.stage.mouseX, Global.stage.mouseY);
        }

        public dispose(): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            loc1 = 0;
            while (!(this._maps == null) && loc1 < this._maps.length) 
            {
                loc2 = this._maps[loc1] as Object /* flash.display.Sprite */;
                ToolTipsManager.unregister(loc2);
                if (loc2.numChildren > 0) 
                {
                    loc3 = loc2.getChildAt(0) as SWFPlayer;
                }
                if (loc3 != null) 
                {
                    loc3.dispose();
                }
                ++loc1;
            }
            this._maps = null;
            while (!(this._bosses == null) && loc1 < this._bosses.length) 
            {
                if ((loc4 = this._bosses[loc1] as SWFPlayer) != null) 
                {
                    loc4.dispose();
                }
                ++loc1;
            }
            this._bosses = null;
            loc1 = 0;
            while (!(this._placeNames == null) && loc1 < this._placeNames.length) 
            {
                if ((loc5 = this._placeNames[loc1] as Object /* flash.display.Bitmap */) != null) 
                {
                    loc5.bitmapData = null;
                }
                ++loc1;
            }
            this._placeNames = null;
            if (this._bg == null) 
            {
            };
            this._bg = null;
            return;
        }

        /* internal */ /* const */MapNamePre: string="lv70DramaMap";

        /* internal */ /* const */MapBgPre: string="lv70DramaMap";

        /* internal */ /* const */MapBossPre: string="lv70DramaMapBoss";

        /* internal */ /* var */_maps: Array<any>;

        /* internal */ /* var */_bosses: Array<any>;

        /* internal */ /* var */_txtNames: Array<any>;

        /* internal */ /* var */_placeNames: Array<any>;

        /* internal */ /* var */_mapData: Array<any>;

        /* internal */ /* var */_callback: Function;

        /* internal */ /* var */_grayDatas: Array<any>;

        /* internal */ /* var */_needReload: Array<any>;

        /* internal */ /* var */_bg: SWFPlayer;

        /* internal */ /* var */_isMapFileGot: Boolean=false;

        /* internal */ /* var */_curFrame: int;

        /* internal */ /* var */_mapInfo: Lv70DramaMapInfo;

        /* internal */ /* var */_curDifficulty: int=2;

        /* internal */ /* var */_curPlaceIndex: int=0;
    }
